package com.example.administrator.shzs.app;

import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Process;
import android.util.Log;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.example.administrator.shzs.utils.CrashHandler;

import org.xutils.x;
import java.util.ArrayList;
import java.util.List;

/**
 * 主程序
 */
public class MyApplication extends Application {

    /**
     * MyApplication实例
     */
    public static MyApplication app;

    //    积分
    private String point =null;
    //用户名
    private String username = null;
    //    用户手机号
    private String phone = null;

    /**
     * 用户Id
     */
    private String userId=null;
    private String userNumber=null;


    /**
     * 商家Id
     */
    private String shopId=null;


    //是否第一次进行定位
    private boolean isFirstIn = true;
    //定位
    private LocationClient mLocationClient;

    private String address=null;
    public double latitude;//纬度

    public double longitude;//经度

    public static final String PARTNER = "2088421583706578";

    //商户收款账户
    public static final String SELLER = "changjiesh@yeah.net";
    // 商户私钥，pkcs8格式
    public static final String RSA_PRIVATE =
            "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMaDjuGHsQTYLSFOr5v11K3Q3/1I1f3/TwI/WRjzPJNMxgldHHUR2DoY8a5H1ndFDFN8Fn3oVdMAcdjuMXQXeDLA+aVJ3hjhZetOCxbvCdzrAKnXKepOaTKb8lFCUVzcomiA8RyGNTrLrF4oeSTahwLqOIx+PEo/8ZkYBJBYCxMLAgMBAAECgYADbQPLOpdAeSaWO5BmRaDM+xzSdy5SOtS3OjakapYyCJImWTgDgTJVFlcdZdn6pvNmgW6174uDn69XSg0KbtlBJygbGKakZ0YaR42+ydBXeckKIm+PGBoA+9egYR4BqN7RxgW5b3YrcbweETpZuMfdGD5Xcdf/2HFvofvCr9g5cQJBAPhrJ3uLoCtrf2dRAOd2EG9n/VW1kVCBjUwcHWtMylMQpxCvXFsDDNlNFvqd+sT8QGmu5rmtlnU69zRhEsG2HNMCQQDMkoIpX5ZhqX1oaz1JpEGz1yoFfCZd2rl936ixlUJwHCKT6D1MGxz+A+DKnveUKiMg64Am6x9u+oUMStYtx23pAkEAg4L+0Pn3HmV3ejO4/dBUHRZ5WFGaD3u2WO+dZ3ustNLhDwU3KHtx5uxX2O+fnX9Jfml4fh32xDMmQdWOP3zwvwJAJW39AMLriezv9CYC7bFvEA+jMaO4hV2aqMw+Wleb81VCgltws7cWQ+fDvm79j49yI/jS1swYUT7nDwu2SmP92QJAEyPb7aXlrT82Hs+4PkDVWfkQIPf8QnU18UDy8/PA438LrHs8PlJZVJaus0MGz2pCqbZVkRk/4QAZCdCFuEMzfA==";

    /**
     * Activity集合
     */
    public List<Activity> activityList=new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();

        app = this;


        //初始化xutils框架
        x.Ext.init(this);
        //初始化百度地图
        SDKInitializer.initialize(this);

        //定位
        setLocation();

        //获取用户信息
        getUserData();


        //全局捕获异常
        CrashHandler crashHandler = new CrashHandler(this);
        Thread.setDefaultUncaughtExceptionHandler(crashHandler);
    }

    /**
     * 纬度
     * @return
     */
    public double getLatitude(){
        return latitude;
    }

    /**
     * 经度
     * @return
     */
    public double getLongitude(){
        return longitude;
    }


    /**
     * 设置商家Id
     * @param shopId
     */
    public void setShopId(String shopId){
        this.shopId=shopId;
    }

    /**
     * 获取商家Id
     * @return
     */
    public String getShopId() {
        return shopId;
    }


    /**
     * 定位参数
     */
    private void setLocation() {

        mLocationClient = new LocationClient(this);

        mLocationClient.registerLocationListener(new MyBDLocation());

        //定位参数
        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
        option.setScanSpan(1000);//一秒请求一次
        mLocationClient.setLocOption(option);

        //开启定位
        mLocationClient.start();

    }


    /**
     * 设置用户id
     * @param userId
     */
    public void setUserId(String userId){
        this.userId=userId;
    }

    /**
     * 获取用户Id
     * @return
     */
    public String getUserId(){
        return userId;
    }


    /**
     * 销毁所有Activity
     */
    public void finishActivity() {

        for (Activity activity : activityList) {
            try {
                //销毁所有activity
                activity.finish();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //杀死当前进程
        Process.killProcess(Process.myPid());
    }


    /**
     * 定位接口
     */
    class MyBDLocation implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //定位成功后的一个回调
            latitude = bdLocation.getLatitude();
            longitude = bdLocation.getLongitude();

            //获取当前所在位置
            if (isFirstIn) {
                address = bdLocation.getAddrStr();
                Log.e("TAG","纬度latitude="+latitude+",经度longitude="+longitude);
                mLocationClient.stop();
                isFirstIn=false;
            }
        }
    }


    /**
     * 获取定位地址
     * @return 定位地址
     */
    public String getAddress() {

        return address;
    }



    //积分
    public void setPoint(String point) {
        this.point = point;
    }

    //积分
    public String getPoint() {
        return point;
    }

//    用户名
    public void setUsername(String username){
        this.username = username;
    }
    public String getUsername(){
        return username;
    }

    //手机号
    public void setPhone(String Phone){
        this.phone=Phone;
    }
    public String getPhone(){
        return phone;
    }


    /**
     * 获取用户账号
     * @return
     */
    public String getUserNumber(){
        return userNumber;
    }


    /**
     * 设置用户账号
     * @param userNumber
     */
    public void setUserNumber(String userNumber){
        this.userNumber=userNumber;
    }



    /**
     * 持久化保存用户账号和名字和userId
     */
    public void saveUserData(){

        SharedPreferences spf=getSharedPreferences("userData", MODE_PRIVATE);
        SharedPreferences.Editor editor=spf.edit();
        editor.putString("userNumber", userNumber);
        editor.putString("userId",userId);
        editor.putString("point",point); //积分
        editor.putString("username",username);
        editor.commit();

    }


    /**
     * 获取用户本人名字级userId
     */
    public void getUserData(){
        //给用户信息赋值
        SharedPreferences pref=getSharedPreferences("userData", MODE_PRIVATE);
        this.userId=pref.getString("userId", null);
        this.userNumber=pref.getString("userNumber", null);
        this.point= pref.getString("point",null);
        this.username= pref.getString("username",null);
    }

}
